package com.zjdiepu.www.commpent.mapper.base;

import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.stereotype.Component;

import com.zjdiepu.www.base.core.BaseMapper;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.DataDicType;
import com.zjdiepu.www.commpent.RedisObjectClient;
import com.zjdiepu.www.dao.base.SysRoleMapper;
import com.zjdiepu.www.model.base.dto.SimpleDataDictionary;
import com.zjdiepu.www.model.base.dto.SysRole;
import com.zjdiepu.www.model.base.dto.SysUser;
import com.zjdiepu.www.util.CachedEnums;
/**
 * 项目名称：diepu-parent-biz <br/> 
 * 类名称：SysUserVoMapper <br/>  
 * 类描述：系统用户 <br/>    
 * 创建人：xiongxiaotun <br/> 
 * 创建时间：2017年4月26日 下午1:33:37  <br/>   
 * 修改人：xiongxiaotun <br/> 
 * 修改时间：2017年4月26日 下午1:33:37   <br/>  
 * 修改备注：    <br/> 
 * @version V1.0   <br/>
 */
@Component
public class SysUserVoMapper extends BaseMapper<SysUser>{
	@Resource
	private RedisObjectClient redisObjectClient;
	@Resource
	private SysRoleMapper sysRoleMapper;

	@Override
	public void mapper(RedisConnection redisConnection, SysUser m) {
		if(null == m) {
			return;
		}
		List<SysRole> sysRoles = sysRoleMapper.selectByUserId(m.getId());
		if(CollectionUtils.isNotEmpty(sysRoles)){
			List<String> sysRoleNames = sysRoles.stream().map(item -> StringUtils.isNotBlank(item.getRoleName()) ? item.getRoleName().trim() : "")
					.filter(item ->StringUtils.isNotBlank(item)).collect(Collectors.toList());
			m.setRoleName(StringUtils.join(sysRoleNames, ","));
		}
		Integer accountType = m.getAccountType();
		SimpleDataDictionary accountTypeDic = redisObjectClient.hGet(redisConnection, SimpleDataDictionary.class.getSimpleName(), accountType == null ? "" : accountType.toString(), DataDicType.ACCOUNT_TYPE.getCode());
		m.setAccountTypeDisplay(accountTypeDic == null ? "" : accountTypeDic.getName());
		
		Integer dpStatus = m.getDpStatus();
		m.setDpStatusDisplay((String)CachedEnums.getValue(dpStatus, DpStatus.class));
	}

}
